#ifndef __QTOSGWIDGET_H__
#define __QTOSGWIDGET_H__

#include <QtWidgets/QOpenGLWidget>
#include <QtGui/QMouseEvent>
#include <QtGui/QWheelEvent>

#include <osgViewer/GraphicsWindow>
#include <osgViewer/Viewer>
#include <osg/Camera>
#include <osg/Material>
#include <osgGA/EventQueue>
#include <osgGA/TrackballManipulator>
#include <osgViewer/ViewerEventHandlers>
#include <osgDB/ReadFile>

namespace YY
{
	class QtOSGWidget : public QOpenGLWidget
	{
	public:
		QtOSGWidget(qreal scaleX, qreal scaleY, QWidget* parent = 0);
		virtual ~QtOSGWidget();

	public:
		void setScale(qreal X, qreal Y);

		osg::ref_ptr<osgViewer::Viewer> getViewer() { return _mViewer; }
		const osg::ref_ptr<osgViewer::Viewer> getViewer() const{ return _mViewer; }

	protected:
		void keyPressEvent(QKeyEvent* event);
		void keyReleaseEvent(QKeyEvent* event);

		virtual void mouseMoveEvent(QMouseEvent* event);
		virtual void mousePressEvent(QMouseEvent* event);
		virtual void mouseReleaseEvent(QMouseEvent* event);
		virtual void wheelEvent(QWheelEvent* event);
		virtual bool event(QEvent* event);

		virtual void paintGL();
		virtual void resizeGL(int width, int height);
		virtual void initializeGL();

	private:
		void setKeyboardModifiers(QInputEvent* event);
		void onHome();
		virtual void onResize(int width, int height);

	private:
		osgGA::EventQueue* getEventQueue() const;

		osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _mGraphicsWindow;
		osg::ref_ptr<osgViewer::Viewer> _mViewer;
		qreal m_scaleX, m_scaleY;

		QPoint selectionStart_;
		QPoint selectionEnd_;

		bool selectionActive_;
		bool selectionFinished_;

		void processSelection();
	};
}

#endif // !__QTOSGWIDGET_H__
